﻿package com.ufenlei.LTS{
	import flash.display.MovieClip;
	import flash.events.ProgressEvent;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.media.SoundTransform;
	import flash.media.SoundChannel;
	
	public class TestingPlayer extends MovieClip{
		private var Pbar:MovieClip;
		private var Lbar:MovieClip;
		private var Tbar:MovieClip;
		private var PHandle:MovieClip;
		private var LHandle:MovieClip;
		private var vs:VolumeSlide;
		private var vb:Switch;
		private var Pbtn:TestingSwitch;
		private var _volume:Number = 1.0;
		public var sf:SoundFacade;
		public function TestingPlayer() {
			sf= new SoundFacade(true, 1000);
			//Pbar = playedBar;
			Lbar = loadedBar;
			Tbar = totalBar;
			PHandle = handle;
			//LHandle = loadedBarTop;
			Pbtn = playBtn;
			vs = volumeSlide;
			//vb = volumeBtn;
			initEvent();
			vs.pecentage = 1.0;
			setPlayed(0);
			setLoaded(0);
		}
		public function stopPlay():void{
			this.sf.stop();
			if(this.sf.s.bytesLoaded < this.sf.s.bytesTotal){
				this.sf.s.close();
			}			
		}

		private function initEvent():void{
			Pbtn.addEventListener(Input.CHANGE, onPlay);
			this.sf.addEventListener(SoundFacade.PLAY_PROGRESS,onPlayProgress);
			this.sf.addEventListener(ProgressEvent.PROGRESS, onLoadProgress);
			this.sf.addEventListener(SoundFacade.PLAY_STOP,onPlayStop);
			this.sf.addEventListener(Event.COMPLETE,onLoadComplete);
			body.addEventListener(MouseEvent.MOUSE_DOWN, onbodyMD);
			vs.addEventListener(Input.CHANGE , onVolumeSlideChange);
		}
		private function onbodyMD(evt:MouseEvent):void{
			this.startDrag();
			(root as MovieClip).addEventListener(MouseEvent.MOUSE_UP, onbodyMU);
		}
		private function onbodyMU(evt:MouseEvent):void{
			this.stopDrag();
		}
		private function onVolumeSlideChange(evt:ProgressEvent):void{
			setVolume(evt.bytesLoaded/evt.bytesTotal);
		}
		private function onVolumeOff(evt:Event):void{
			_volume = this.sf.sc.soundTransform.volume;
			trace("Off",_volume);
			setVolume(0);
		}
		private function onVolumeOn(evt:Event):void{
			setVolume(_volume);
			trace("on",_volume);
		}
		private function setVolume(v:Number = 1):void{
			sf.volume = v;
		}
		private function onPlayProgress(evt:ProgressEvent):void{
			var prg:Number = ((evt.bytesLoaded / evt.bytesTotal)>0.999)?0:(evt.bytesLoaded / evt.bytesTotal);
			setPlayed(prg);
		}

		private function onLoadComplete(evt:Event):void{
			setLoaded(1.0);
		}
		private function onLoadProgress(evt:ProgressEvent):void{
			setLoaded(evt.bytesLoaded/evt.bytesTotal);
		}
		//在这里显示加载进度
		private function setLoaded(pgs:Number):void{
			this.Lbar.width = this.Tbar.width*pgs ;
		}
		//显示播放进度
		private function setPlayed(pgs:Number):void{
			this.PHandle.x = this.Tbar.width*pgs + Tbar.x;
		}
		private function onPlayStop(evt:Event):void{
			setPlayed(0);
		}
		private function onPlay(evt:Event):void{
			((evt.target as Input).getValue())?this.sf.resume():this.sf.pause();
		}
		public function load(soundURL:String):void{
			this.sf.load(soundURL);
			Pbtn.showSelect();
		}
	}
}